package tabdulin.sms.visual;

import java.awt.Dimension;
import java.awt.Rectangle;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;

import tabdulin.sms.contacts.Contact;
import tabdulin.sms.contacts.ContactList;
import tabdulin.sms.contacts.ContactListModel;

import java.awt.Font;

/**
 * @author Talgat Abdulin
 * 
 */
@SuppressWarnings("serial")
public class ContactsDialog extends JDialog {
    private SmsSenderFrame owner;
    private JPanel contactsContentPane;
    private JLabel statusLabel;
    private JList list;
    private JButton selectButton;
    private JButton editButton;
    private JButton deleteButton;
    private JButton closeButton;
    
    private ContactList contactList;
    
    public ContactsDialog(SmsSenderFrame owner, ContactList contactList) {
        super(owner);
        this.owner = owner;
        this.contactList = contactList;
        initialize();
    }

    public void addContact(Contact contact) {
        ((ContactListModel) getContactsList().getModel()).addElement(contact);
    }

    /**
     * This method initializes clostContactsDialogButton
     * 
     * @return javax.swing.JButton
     */
    private JButton getCloseContactsDialogButton() {
        if (closeButton == null) {
            closeButton = new JButton();
            closeButton.setBounds(new Rectangle(234, 263, 99, 27));
            closeButton.setFont(new Font("UnDotum", Font.BOLD, 12));
            closeButton.setText("Закрыть");
            closeButton.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseClicked(java.awt.event.MouseEvent e) {
                    statusLabel.setText(null);
                    ContactsDialog.this.setVisible(false);
                }
            });
        }
        return closeButton;
    }

    /**
     * This method initializes contactsContentPane
     * 
     * @return javax.swing.JPanel
     */
    private JPanel getContactContentPane() {
        if (contactsContentPane == null) {
            statusLabel = new JLabel();
            statusLabel.setBounds(new Rectangle(8, 300, 320, 16));
            statusLabel.setFont(new Font("UnDotum", Font.PLAIN, 12));
            contactsContentPane = new JPanel();
            contactsContentPane.setLayout(null);
            contactsContentPane.add(getContactsList(), null);
            contactsContentPane.add(getSelectContactButton(), null);
            contactsContentPane.add(getEditContactButton(), null);
            contactsContentPane.add(getDeleteContactButton(), null);
            contactsContentPane.add(getCloseContactsDialogButton(), null);
            contactsContentPane.add(statusLabel, null);
        }
        return contactsContentPane;
    }

    /**
     * This method initializes contactsList
     * 
     * @return javax.swing.JList
     */
    public JList getContactsList() {
        if (list == null) {
            list = new JList(new ContactListModel(contactList));
            list.setBounds(new Rectangle(8, 7, 213, 288));
            list.setFont(new Font("UnDotum", Font.PLAIN, 12));
        }
        return list;
    }

    /**
     * This method initializes deleteContactButton
     * 
     * @return javax.swing.JButton
     */
    private JButton getDeleteContactButton() {
        if (deleteButton == null) {
            deleteButton = new JButton();
            deleteButton.setBounds(new Rectangle(232, 73, 99, 30));
            deleteButton.setFont(new Font("UnDotum", Font.BOLD, 12));
            deleteButton.setText("Удалить");
            deleteButton.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseClicked(java.awt.event.MouseEvent e) {
                    if (getContactsList().isSelectionEmpty()) {
                        statusLabel.setText("Необходимо выбрать контакт.");
                        return;
                    }
                    int selectedIndex = getContactsList().getSelectedIndex();
                    ((ContactListModel) getContactsList().getModel()).removeElementAt(selectedIndex);
                    getContactsList().setSelectedIndex(selectedIndex > 0 ? selectedIndex - 1 : 0);
                }
            });
        }
        return deleteButton;
    }

    /**
     * This method initializes editContactButton
     * 
     * @return javax.swing.JButton
     */
    private JButton getEditContactButton() {
        if (editButton == null) {
            editButton = new JButton();
            editButton.setBounds(new Rectangle(230, 40, 102, 25));
            editButton.setFont(new Font("UnDotum", Font.BOLD, 12));
            editButton.setText("Редактировать");
        }
        return editButton;
    }

    /**
     * This method initializes selectContactButton
     * 
     * @return javax.swing.JButton
     */
    private JButton getSelectContactButton() {
        if (selectButton == null) {
            selectButton = new JButton();
            selectButton.setBounds(new Rectangle(232, 9, 100, 26));
            selectButton.setFont(new Font("UnDotum", Font.BOLD, 12));
            selectButton.setText("Выбрать");
            selectButton.addMouseListener(new java.awt.event.MouseAdapter() {
                public void mouseClicked(java.awt.event.MouseEvent e) {
                    if (getContactsList().isSelectionEmpty()) {
                        statusLabel.setText("Необходимо выбрать контакт.");
                        return;
                    }
                    Contact c = (Contact) getContactsList().getSelectedValue();
                    owner.setOperator(c.getOperator());
                    owner.setPrefix(c.getPrefix());
                    owner.setNumber(c.getNumber());
                    statusLabel.setText(null);
                }
            });
        }
        return selectButton;
    }

    private void initialize() {
        setSize(new Dimension(350, 350));
        setLocation(this.owner.getX() + this.owner.getWidth(), this.owner.getY());
        setTitle("Контакты");
        setContentPane(getContactContentPane());
    }
}
